package com.project.cris.SessionBean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;


import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.PersistenceContext;
import com.project.cris.SessionBean.Search_PropertyBeanRemote;
import com.project.entity.Property_Image;
@Stateless
@LocalBean
public  class Search_PropertyBean implements Search_PropertyBeanRemote {
	@PersistenceContext
	EntityManager em;
	@Override
	public String getRandomIMGPath(String propID) throws Exception {
		// TODO Auto-generated method stub
		Query query = em.createNamedQuery("Property_Image.findIMGPath");
		query.setParameter("propID", propID);
		query.setMaxResults(6);
		@SuppressWarnings("unchecked")
		List<String> result = query.getResultList();
		try {
			if (result.size() >= 1) {
				long i = Math.round(Math.random() * result.size() - 0.5);
				String s = result.get((int) i);
				return s;

			} else
				return "/uploaded/PropertyImages/notfound.jpg";
		} catch (Exception e) {
			e.printStackTrace();
			return "/uploaded/PropertyImages/notfound.jpg";
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Property_Image> findImageByPropID(String propID) {
		Query query=em.createQuery("SELECT img.path FROM Property_Image img WHERE img.property.propID = :propID");
		query.setParameter("propID", propID);
		return (ArrayList<Property_Image>) query.getResultList();
	}
	
}
